$(function () {
    userDatatableSource.init();
    pageSource.init();
});

var userDatatable;

/**
 * 表格列定义
 *
 * @type {*[]}
 */
var tableColumns = [{
    field: "id",
    title: "#",
    width: 20,
    textAlign: 'center',
    selector: {
        class: "m-checkbox--solid m-checkbox--brand"
    }
},
    {field: "name", title: "模型名称", textAlign: 'center', sortable: false},
    {field: "mark", title: "备注", textAlign: 'center', sortable: false},
    {field: "createuser", title: "创建人", textAlign: 'center', sortable: false},
    {field: "createtime", title: "创建时间", textAlign: 'center', sortable: false}];

/**
 * 初始化表格定义
 */
var userDatatableSource = {
    init: function () {
        userDatatable = $("#user_data").mDatatable(getMDatatableOption("list", {}, tableColumns));
    },
    deleteById: function (ids) {
        $.hcPostAjax({
            showLoad: true,
            msgPrefix: '模型删除失败：',
            type: 'delete',
            url: basePath + '/model/' + ids,
            success: function (json) {
                layer.msg('模型删除成功！', {
                    icon: 1, time: 1500
                }, function () {
                    userDatatable.reload();
                });
            }
        });
    }
};

var pageSource = {
    init: function () {
        /**
         * 查询
         */
        $("#m_search").on("click", function () {
            var param = {
                name: $.trim($("input[name='name']").val()),
                createuser: $.trim($("input[name='createuser']").val())
            };
            queryMDatatable(userDatatable, param);
        });

        /**
         * 新增
         */
        $("#m_add").on("click", function () {
            openEditUser('添加模型', null);
        });

        /**
         * 修改数据
         */
        $("#editBtn").on("click", function () {
            var ids = getMDatatableSelectedIds(userDatatable);
            if (ids.length > 1) {
                layer.msg('只能选中一条数据进行修改！', {icon: 5, anim: 6});
                return null;
            } else if (ids.length < 1) {
                layer.msg('请选中一条数据进行修改！', {icon: 5, anim: 6});
                return null;
            }
            var id = ids[0];
            openEditUser('修改模型', id);
        });

        /**
         * 批量删除
         */
        $("#deleteBtn").on("click", function () {
            var ids = getMDatatableSelectedIds(userDatatable);
            if (ids.length === 0) {
                layer.msg('请勾选要删除的模型信息！', {
                    icon: 0
                });
                return;
            }
            var userid = getMDatatableSelectedColumns(userDatatable, "userid");
            layer.confirm('确定批量删除所选模型？', {
                icon: 3,
                title: '提示',
                btn: ['确定', '取消'] // 按钮
            }, function () {
                userDatatableSource.deleteById(ids.join(','));
            }, function (index) {
                layer.close(index);
            });
        });

        /**
         * 修改密码
         */
        $("#editPasswordBtn").on("click", function () {
            var ids = getMDatatableSelectedIds(userDatatable);
            if (ids.length > 1) {
                layer.msg('只能选中一条数据进行修改！', {icon: 5, anim: 6});
                return null;
            } else if (ids.length < 1) {
                layer.msg('请选中一条数据进行修改！', {icon: 5, anim: 6});
                return null;
            }
            var id = ids[0];
            var names = getMDatatableSelectedColumns(userDatatable, "name");
            openEditPassword('修改密码', id, names);
        });

        /**
         * 重置密码
         */
        $("#resetWord").on('click', function () {
            var ids = getMDatatableSelectedIds(userDatatable);
            if (ids.length > 1) {
                layer.msg('只能选中一条数据进行重置！', {icon: 5, anim: 6});
                return null;
            } else if (ids.length < 1) {
                layer.msg('请选中一条数据进行重置！', {icon: 5, anim: 6});
                return null;
            }
            var id = ids[0];
            var names = getMDatatableSelectedColumns(userDatatable, "name");
            layer.confirm('确定重置该用户<font color="red">[' + names + ']</font>的密码？', {
                icon: 3,
                title: '提示',
                btn: ["确认", "取消"]
            }, function (index) {
                $.hcPostAjax({
                    showLoad: true,
                    url: basePath + '/wQyUser/resetWord',
                    data: {
                        id: id
                    },
                    success: function (data) {
                        layer.msg('重置默认密码成功,请用户<font color="red">[' + names + ']</font>重新登入！', {
                            icon: 1, time: 1500
                        }, function () {
                            userDatatable.reload();
                            layer.closeAll();
                        });
                    },
                    error: function () {
                        layer.msg('重置失败');
                    }
                });
            })
        });
    }
};

/**
 * 新增和修改
 * @param title
 * @param obj
 */
function openEditUser(title, id) {
    var msg = isEmpty(id) ? "新增" : "编辑";
    layer.open({
        title: title,
        type: 1,
        maxmin: true,
        area: ['60%', '70%'],
        shadeClose: true,
        scrolling: false,
        content: $("#editHtml").html(),
        success: function (layero, index) {
            if (!isEmpty(id)) {
                var newData=getDetail(id)
                renderFormData("#user-form",newData );
                $("#modelName").val(newData.name);
            }
            //保存
            $("#edit_yes").on("click", function () {
                form.on('submit(save_user)', function (data) {
                    var param = getFormData("#user-form");
                    param.name = $.trim($("input[name=modelName]").val());
                    if (!isEmpty(id)) {
                        param.id = id;
                    } else {
                        param.mark = $.trim($("input[name=mark]").val());
                    }
                    $.hcPostAjax({
                        showLoad: true,
                        msgPrefix: msg + "出错：",
                        url: basePath + '/model',
                        data: param,
                        success: function (data) {
                            layer.msg(msg + '成功！', {
                                icon: 1, time: 1500
                            }, function () {
                                userDatatable.reload();
                                layer.closeAll();
                            });
                        }
                    });
                });
            });
            //取消修改
            $("#edit_cancle").on("click", function () {
                layer.closeAll();
            });
        }
    });
}

function getDetail(id) {
    var oldData;
    $.hcPostAjax({
        type: 'get',
        url: basePath + "/model/detail",
        data: {
            id: id
        },
        async: false,
        success: function (data) {
            oldData = data;
        }
    });
    return oldData;
}

